/**
 * 
 */
package com.jie.eclipse.pan.popup.wizardpage;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntities;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.editors.utils.PanImageHelper;
import com.jie.eclipse.pan.listener.PMouseListener;
import com.jie.eclipse.pan.popup.wizard.CreatePageTemplateWizard;
import com.jie.eclipse.pan.swtitem.table.PTable;
import com.jie.eclipse.pan.swtitem.table.PTableColumn;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class CreatePageTemplate2 extends BasePage{
	PTable candidateTable = null;
	/**
	 * @param pageName
	 */
	public CreatePageTemplate2() {
		super(CreatePageTemplateWizard.page_2,"",PanImageHelper.getInstance().getPanImageDescriptor());
		//设置标题消息
		this.setMessage("查询模型配置:");
	}
	
	
	@Override
	public void createControl(Composite parent) {
		BaseDataBind dataBind = this.getDataBind();
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, true));
		
		SashForm sashForm = new SashForm(composite, SWT.VERTICAL);
		sashForm.setLayoutData((new GridData(GridData.FILL_BOTH)));
		
		
		Group group1 = new Group(sashForm, SWT.NULL);
		group1.setText("数据源"); 
		group1.setLayout(new GridLayout(3, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label labe11 = new Label(group1, SWT.RIGHT);
		labe11.setText("DataSource:");
		Combo combo1 = new Combo(group1, SWT.READ_ONLY);
		String[] datasources = QMDao.getPanAllDataSource(false);
		combo1.setItems(datasources);
		combo1.select(1);
		combo1.setLayoutData(new GridData(100, 24));
		for (int i = 0; i < datasources.length; i++) {
			combo1.setData(String.valueOf(i), datasources[i]);
		}
		dataBind.bind("dataSource", combo1);
		
		Button button1 = new Button(group1, SWT.NONE);
		button1.setText("语句分析");
		button1.addMouseListener(new PMouseListener() {
			public void mouseUp(MouseEvent paramMouseEvent) {
				QMDao qmDao = QMDao.getInstance();
				candidateTable.ReLoadRecord(qmDao.getColumnsInfo(getDataBind().getValue("dataSource"),getDataBind().getValue("sql")));
			}
		});
		
		Label labe12 = new Label(group1, SWT.RIGHT);
		labe12.setText("SQl:");
		Text text2 = new Text(group1, SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
		GridData text2GridData = new GridData(GridData.FILL_HORIZONTAL);
		text2GridData.horizontalSpan=2;
		text2GridData.heightHint = 50;
		text2.setLayoutData(text2GridData);
		dataBind.bind("sql", text2);
		
		Label labe13 = new Label(group1, SWT.RIGHT);
		labe13.setText("可选栏位:");
		candidateTable = newCandidateTable(group1);
		
		Group group2 = new Group(sashForm, SWT.NULL);
		group2.setText("模板属性2"); 
		group2.setLayout(new GridLayout(2, false));
		group2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		sashForm.setWeights(new int[]{2,3});
		setControl(composite);
	}
	
	public PTable newCandidateTable(Composite parent){
		PTable table = new PTable(parent, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL|SWT.MULTI|SWT.FULL_SELECTION);
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		tableGridData.horizontalSpan=2;
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("columnName");
		col1.setWidth(60);
		
		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		col2.setWidth(60);
		
		table.reloadColumns();
		return table;
	}

}
